<?php

require_once 'config.php';

require_once 'includes/functions.php';

$apps = getApps();

$popularKeywords = ['游戏', '工具', '社交', '学习', '娱乐', '办公'];

?>

<?php include 'includes/header.php'; ?>

<!-- 搜索区域 -->

<div style="background: white; padding: 40px 0; margin-bottom: 30px; border-radius: 10px;">

    <div class="container">

        <div style="text-align: center;">

            <h2 style="margin-bottom: 20px; color: #333;">发现优质手机应用</h2>

            <div class="search-container">

                <form action="search.php" method="GET" class="search-form">

                    <input type="text" 

                           name="q" 

                           placeholder="输入应用名称、介绍或更新内容..." 

                           class="search-input"

                           value="<?php echo isset($_GET['q']) ? htmlspecialchars($_GET['q']) : ''; ?>">

                    <button type="submit" class="search-btn">搜索</button>

                </form>

            </div>

            

            <!-- 热门搜索 -->

            <div class="quick-search">

                <p style="color: #666; margin-bottom: 10px;">热门搜索：</p>

                <div class="search-tags">

                    <?php foreach ($popularKeywords as $keyword): ?>

                        <a href="search.php?q=<?php echo urlencode($keyword); ?>" class="search-tag">

                            <?php echo htmlspecialchars($keyword); ?>

                        </a>

                    <?php endforeach; ?>

                </div>

            </div>

        </div>

    </div>

</div>

<!-- 免责声明 -->

<div class="disclaimer">

    <h3>免责声明</h3>

    <ul>

        <?php foreach (getDisclaimer() as $item): ?>

            <li><?php echo $item; ?></li>

        <?php endforeach; ?>

    </ul>

</div>

<!-- 应用列表 -->

<h2 style="margin: 30px 0 20px 0;">应用列表</h2>

<?php if (empty($apps)): ?>

    <div style="text-align: center; padding: 40px; background: white; border-radius: 10px;">

        <p>暂无应用，请管理员添加应用</p>

    </div>

<?php else: ?>

    <div class="apps-grid">

        <?php foreach ($apps as $app): ?>

            <div class="app-card">

                <?php if (!empty($app['icon'])): ?>

                    <img src="<?php echo htmlspecialchars($app['icon']); ?>" 

                         alt="<?php echo htmlspecialchars($app['name']); ?>" 

                         class="app-icon">

                <?php endif; ?>

                <div class="app-name">

                    <a href="detail.php?id=<?php echo $app['id']; ?>" 

                       style="color: inherit; text-decoration: none;">

                        <?php echo htmlspecialchars($app['name']); ?>

                    </a>

                </div>

                <div class="app-version">版本: <?php echo htmlspecialchars($app['version']); ?></div>

                <div class="app-description"><?php echo htmlspecialchars($app['description']); ?></div>

                

                <div style="display: flex; gap: 10px; margin-top: 10px;">

                    <a href="detail.php?id=<?php echo $app['id']; ?>" 

                       class="btn btn-secondary" 

                       style="flex: 1; text-align: center; padding: 8px 12px;">

                        查看详情

                    </a>

                    <a href="<?php echo htmlspecialchars($app['download_url']); ?>" 

                       class="btn" 

                       style="flex: 1; text-align: center; padding: 8px 12px;"

                       target="_blank">

                        下载

                    </a>

                </div>

                

                <?php if (isLoggedIn()): ?>

                    <div style="margin-top: 10px; display: flex; gap: 5px;">

                        <a href="admin.php?edit=<?php echo $app['id']; ?>" 

                           class="btn btn-secondary" 

                           style="padding: 5px 10px; font-size: 12px; flex: 1;">

                            编辑

                        </a>

                        <form action="action/delete_app.php" method="POST" style="flex: 1;">

                            <input type="hidden" name="id" value="<?php echo $app['id']; ?>">

                            <button type="submit" 

                                    class="btn btn-danger" 

                                    style="padding: 5px 10px; font-size: 12px; width: 100%;"

                                    onclick="return confirm('确定删除这个应用吗？')">

                                删除

                            </button>

                        </form>

                    </div>

                <?php endif; ?>

            </div>

        <?php endforeach; ?>

    </div>

<?php endif; ?>

<?php include 'includes/footer.php'; ?>